package com.fundboss.display;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import model.market.MarketConfig;

public class AssetsParamPanel extends JPanel{

	public AssetsParamPanel(final MarketConfig config){
		
		setLayout(new GridLayout(0,2));
		JLabel title = new JLabel("Assets");
		title.setFont(new Font("Arial",Font.BOLD,18));
		add(title);
		
		add(new JLabel(""));
		final JComboBox numberOfAssetsCombo = new JComboBox(new Object[]{"1","2","3","5","10"});
		add(new JLabel("Number in market:"));
		config.setNumberOfAssets(5);
		numberOfAssetsCombo.setSelectedItem("5");
		add(numberOfAssetsCombo);
		numberOfAssetsCombo.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				int numberOfAssets = Integer.parseInt((String)numberOfAssetsCombo.getSelectedItem());
				config.setNumberOfAssets(numberOfAssets);
			}
		});
		
		
		
		
		add(new JLabel("Cashflow Volatility"));
		final JComboBox volatility = new JComboBox(new Object[]{"0%","10%","20%","30%","50%","80%"});
		volatility.setSelectedItem("30%");
		config.setVolatility(.3);
		volatility.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Number number = getPercent(volatility.getSelectedItem());
			   config.setVolatility((Double)number);
			    
				
			}

			
		});
		add(volatility);
		
		add(new JLabel("Cashflow Volatility Asset 0"));
		final JComboBox volatility0 = new JComboBox(new Object[]{"0%","10%","20%","30%","50%","80%"});
		volatility0.setSelectedItem("10%");
		config.setVolatilityAsset0(.1);
		volatility0.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Number number = getPercent(volatility0.getSelectedItem());
			   config.setVolatilityAsset0((Double)number);
				
			}
			
		});
		add(volatility0);
		
		
		add(new JLabel("Random Seed Set"));
		final JComboBox seed = new JComboBox(new Object[]{"1","2"});
		seed.setSelectedItem("1");
		config.setSeedRandom(1);
		seed.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				config.setSeedRandom(Integer.parseInt((String)seed.getSelectedItem()));
			}
		});
		add(seed);
		
	}
	
	public static Number getPercent(Object selectedItem){
		Locale locale = Locale.ENGLISH;
		try {
			Number number = NumberFormat.getPercentInstance(locale).parse(
					(String) selectedItem);
			// 1.2345
			if (number instanceof Long) {
				Long tempNumber = (Long) number;
				number = new Double(tempNumber.intValue());
			}
			return number;
		} catch (ParseException ex) {
	        throw new RuntimeException(ex);
	    }
		
	}
}
